package com.sun.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.pojo.Menu;
import com.sun.pojo.Role;
import com.sun.service.MenuService;
import com.sun.service.RoleService;
import com.sun.utils.DataGridViewResult;
import com.sun.utils.SystemConstant;
import com.sun.utils.TreeNode;
import com.sun.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/list")
    public DataGridViewResult findRoleListByPage(RoleVo roleVo) {
        // 设置分页信息
        PageHelper.startPage(roleVo.getPage(), roleVo.getLimit());
        // 调用方法查询部门数据
        List<Role> roleListByPage = roleService.findRoleListByPage(roleVo);
        // 创建分页对象
        PageInfo<Role> deptPageInfo = new PageInfo<Role>(roleListByPage);
        return new DataGridViewResult(deptPageInfo.getTotal(), deptPageInfo.getList());
    }

    @RequestMapping("/addRole")
    public String addRole(Role role) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = roleService.addRole(role);
        if (i > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, role.getRoleName() + " 添加成功!");
        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, role.getRoleName() + " 添加失败!");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/updateRole")
    public String updateRole(Role role) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = roleService.updateRole(role);
        if (i > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, role.getRoleName() + " 修改成功!");
        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, role.getRoleName() + " 修改失败!");
        }
        return JSON.toJSONString(map);
    }

    @PostMapping("/deleteRole")
    public String deleteRole(int roleId) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int i = roleService.deleteRole(roleId);
        if (i > 0) {
            map.put(SystemConstant.SUCCESS, true);
            map.put(SystemConstant.MESSAGE, "删除成功!");
        } else {
            map.put(SystemConstant.SUCCESS, false);
            map.put(SystemConstant.MESSAGE, "删除失败!");
        }
        return JSON.toJSONString(map);
    }

    @Autowired
    private MenuService menuService;

    @RequestMapping("/initMenuTree")
    public DataGridViewResult initMenuTree(int roleId) {
        /**
         * 1、查出所有菜单
         */
        List<Menu> allMenu = menuService.findAllMenu();
        /**
         * 2、查出角色拥有的菜单
         */
        List<Integer> menuListByRoleId = menuService.findMenuListByRoleId(roleId);
        /**
         * 3、返回数据
         */
        List<TreeNode> dtreeNodes = new ArrayList<TreeNode>();
        /**
         * 4、设置默认选中
         */
        for (Menu menu : allMenu) {
            String checkArr = "0";
            for (Integer integer : menuListByRoleId) {
                if (menu.getId() == integer) {
                    checkArr = "1";
                    break;
                }
            }
            Boolean spread = (menu.getSpread() == null || menu.getSpread() == 1) ? true : false;
            dtreeNodes.add(new TreeNode(menu.getId(), menu.getPid(), menu.getTitle(), spread, checkArr));
        }
        return new DataGridViewResult(dtreeNodes);
    }

    @PostMapping("/addRoleMenu")
    public String addRoleMenu(int roleId, String menuIds) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        if (roleService.addRoleMenu(roleId, menuIds)) {
            map.put(SystemConstant.MESSAGE, "菜单保存成功!");
        } else {
            map.put(SystemConstant.MESSAGE, "菜单保存失败!");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/findRoleByEmployeeId")
    public DataGridViewResult findRoleByEmployeeId(int id) {
//        1 查询所有角色的map集合
        List<Map<String, Object>> roleList = roleService.findRoles();

//        2 查询该员工拥有的角色ID
        List<Integer> roleIds = roleService.findRoleIdsByEmployeeId(id);

//        3 判断所有角色中属于员工角色 添加LAY_CHECKED
        for (Map<String, Object> map : roleList) {
            Boolean lay_checked = false;
            int base_role_id = (Integer) map.get("id");
            for (Integer roleId : roleIds) {
                if (base_role_id == roleId) {
                    lay_checked = true;
                    break;
                }
            }
            map.put("LAY_CHECKED", lay_checked);
        }
        return new DataGridViewResult((long) roleList.size(), roleList);
    }
}
